package cn.edu.ujn.booksshop.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booksshop.dao.ShoppingCart;
import cn.edu.ujn.booksshop.service.IShoppingCartService;
import cn.edu.ujn.util.Page;

@Controller
public class ShoppingCartController {

	@Autowired
	private IShoppingCartService shoppingCart;
	
	@GetMapping("/shoppingCart/list.action")
	public String list(Model model,@RequestParam (defaultValue = "1")  Integer page ,@RequestParam(defaultValue ="10")  Integer rows) {
		Page<ShoppingCart> pages = this.shoppingCart.selectAll(page,rows);
		model.addAttribute("page",pages);
		return "shoppingCart";
	}
	
	@GetMapping("/shoppingCart/getShoppingCartById.action")
	@ResponseBody
	public ShoppingCart list1(@RequestParam Integer id) {
		ShoppingCart cart = shoppingCart.selectByPrimaryKey(id);
		return cart;
	}
	
	
	@PostMapping("/shoppingCart/update.action")
	public ResponseEntity<String> list2(@ModelAttribute ShoppingCart sc) {
		int key = shoppingCart.updateByPrimaryKey(sc);
		if(key==1) {
			return ResponseEntity.ok("OK");
		}
		return ResponseEntity.ok("NO");
	}
	
	@PostMapping("/shoppingCart/delete.action")
	public ResponseEntity<String> delete(@RequestParam Integer id) {
		int key2 = shoppingCart.deleteByPrimaryKey(id);	
		if(key2 == 1) {
			return ResponseEntity.ok("OK");
		}
		return ResponseEntity.ok("NO");
	}
	
	
	@PostMapping("/shoppingCart/create.action")
	public ResponseEntity<String> create(@ModelAttribute ShoppingCart sc){
		int insert = shoppingCart.insert(sc);
		if(insert == 1) {
			return ResponseEntity.ok("OK");
		}
		return ResponseEntity.ok("NO");
	}
}
